<?php
/**
 * User: zsj
 * Date: 2020/7/2
 * Time: 14:08
 */

namespace app\common\libray\traits;


use think\facade\Db;
use think\facade\View;

trait Admin
{
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit,$field) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->field($field)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return View::fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->param('row/a', []);
            //异常接管
            if ($this->modelValidate) {
                $currentNameSpace = get_class($this->model);
                $currentNameSpace = str_replace(['common', 'model'], ['admin', 'validate'], $currentNameSpace);
                if (class_exists($currentNameSpace)) {
                    $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $currentNameSpace . '.add' : $currentNameSpace) : $this->modelValidate;
                    $this->validate($params, $validate);
                }
            }
            $result = $this->model->save($params);
            if ($result) {
                $this->success('操作成功');
            }
            $this->error('操作失败');
        }
        return View::fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error('未找到相关数据');
        }
        View::assign('row', $row);
        if ($this->request->isPost()) {
            $params = $this->request->param('row/a', []);
            //异常接管
            if ($this->modelValidate) {
                $currentNameSpace = get_class($this->model);
                $currentNameSpace = str_replace(['common', 'model'], ['admin', 'validate'], $currentNameSpace);
                if (class_exists($currentNameSpace)) {
                    $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $currentNameSpace . '.edit' : $currentNameSpace) : $this->modelValidate;
                    $this->validate($params, $validate);
                }
            }
            $result = $row->save($params);
            if ($result) {
                $this->success('更新成功');
            }
            $this->error('更新失败');
        }
        return View::fetch();
    }

    /**
     * 删除
     */
    public function del($ids = null)
    {
        $pk = $this->model->getPk();
        $list = $this->model->where($pk, 'in', $ids)->select();
        Db::transaction(function () use ($list) {
            foreach ($list as $k => $v) {
                $v->delete();
            }
        });
        $this->success('删除成功');
    }

    /**
     * 回收站
     */
    public function recycleBin()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->onlyTrashed()
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->onlyTrashed()
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return View::fetch();
    }

    /**
     * 真实删除
     */
    public function destroy($ids = null)
    {
        Db::transaction(function () use ($ids) {
            if ($ids) {
                $ids = explode(',', $ids);
                $list = $this->model->onlyTrashed()->select($ids);
            } else {
                $list = $this->model->onlyTrashed()->select();
            }
            foreach ($list as $index => $item) {
                $item->force()->delete(true);
            }
        });
        $this->success('删除成功');
    }

    /**
     * 还原
     */
    public function restore($ids = null)
    {
        Db::transaction(function () use ($ids) {
            if ($ids) {
                $ids = explode(',', $ids);
                $list = $this->model->onlyTrashed()->select($ids);
            } else {
                $list = $this->model->onlyTrashed()->select();
            }
            foreach ($list as $index => $item) {
                $item->restore();
            }
        });
        $this->success('恢复成功');
    }

    /**
     * 批量更新
     */
    public function multi($ids = "")
    {
        $ids = $ids ? $ids : $this->request->param("ids");
        if ($ids) {
            if ($this->request->has('params')) {
                parse_str($this->request->post("params"), $values);
                if ($values) {
                    Db::transaction(function () use ($ids, $values) {
                        $ids = explode(',', $ids);
                        $list = $this->model->select($ids);
                        foreach ($list as $item) {
                            $item->save($values);
                        }
                    });
                    $this->success('更新成功');
                } else {
                    $this->error('未知条件，无法更新');
                }
            } else {
                $this->error('未知条件，无法更新');
            }
        } else {
            $this->error('参数异常');
        }
    }
}